<?php
/*
 * 留言
 */
namespace app\home\controller;

use app\common\controller\IndexBase;
use think\facade\Session;
use think\facade\Db;

class Mail extends IndexBase
{
    protected function initialize()
    {
        parent::initialize();
    }
    /*
     * 意见
     */
    public function index()
    {   
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@login'));
        if ($this->request->isPost())
        {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0')
            {
                try{
                    $this->validate($param, 'mail');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $data = ['content' => $param['content'],'types' => $param['types'],'from_id' => session('user_auth.user_id'),'to_name'=>'admin','addtime'=>time()];
            $save = Db::table('mail')->insert($data);
            insert_user_log($uid,'发送了留言');
            !$save && $this->error('留言失败！',url('@mail'));
            $this->success('留言成功！', url('@mail'));
        }
        return $this->fetch('',['data'=>DB::table('mail')->where('from_id',$uid)->order('addtime','DESC')->paginate(),'info'=>DB::table('user')->find($uid)]);
    }
}
